<?php
/**
 * Application level View Helper
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.View.Helper
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Helper', 'View');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class AppHelper extends Helper {
    
    public $helpers = array('Html', 'Form');
    
    public function buttonLink($label, $href, $htmlOptions=array(), $confirm=false) {
        if (is_array($href)) {
            $href = Router::url($href);            
        }
        if (preg_match('/^javascript:/i', $href)) {
            $onclick = preg_replace('/^javascript:/i', '', $href);
        }else{
            $onclick = "location.href='{$href}'";
        }
        
        if (! empty($confirm)) {
            $onclick = "if(! confirm('{$confirm}')){return};{$onclick}";
        }
        
        $options = array_merge($htmlOptions, array(
            'type'    => 'button',
            'onclick' => $onclick
        ));
        return $this->Form->button($label, $options);
    }
    
    public function buttonLinkConfirm($label, $href, $htmlOptions=array(), $confirm) {
        return $this->buttonLink($label, $href, $htmlOptions, $confirm);
    }
    
    public function formError($id, $message=null) {
        return '<span class="error-container">' .
            $this->Form->error($id, $message, array( 'wrap' => 'span' )) .
            '</span>';
    }
    
    public function autocomplete($id, $options) {
        return $this->Form->hidden( $id, array(
            'value' => $options['value']
        )) . $this->Form->text($id . "_search", array(
            'value'  => $options['label'],
            'class'  => 'autocomplete large',
            'ac:url' => $options['url'],
            'ac:select' => @$options['onSelect'],
            'ac:search' => @$options['onSearch']
        ));
    }
}
